﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;

namespace Waker
{
    public partial class FormLEDInDoorDisplay : Base.FormBase
    {
        public FormLEDInDoorDisplay()
        {
            InitializeComponent();
        }

        List<Label> _ListCurrtimeLabel;
        PictureBox _PicEventPhoto = null;
        List<Label> _ListEventLabel = null;
        private void FormLEDInDoor_Load(object sender, EventArgs e)
        {
            try
            {
                Screen screenPrev = null;
                if (Consts.LEDInDoorScreenIndex < 1)
                    screenPrev = Screen.AllScreens[0];
                else
                    screenPrev = Screen.AllScreens[Consts.LEDInDoorScreenIndex - 1];
                var screen = Screen.AllScreens[Consts.LEDInDoorScreenIndex];
                //this.Location = new Point(screenPrev.WorkingArea.Width, screenPrev.WorkingArea.Height);
                this.Location = screen.WorkingArea.Location;
                this.Size = new System.Drawing.Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
                foreach (var en in Session.ListInDoorLEDData)
                {

                    if (en.LEDType == Convert.ToInt32(Enums.Enum_LEDType.EventPhoto))
                    {
                        _PicEventPhoto = new PictureBox();
                        _PicEventPhoto.Location = new Point(en.LocationX * 2, en.LocationY * 2);
                        _PicEventPhoto.Size = new Size(300 * 2, 200 * 2);

                        _PicEventPhoto.SizeMode = PictureBoxSizeMode.Zoom;
                        _PicEventPhoto.BorderStyle = BorderStyle.FixedSingle;
                        this.plContent.Controls.Add(_PicEventPhoto);

                        if (_ListEventLabel == null)
                        {
                            _ListEventLabel = new List<Label>();
                            timerEvent.Enabled = true;
                            timerEvent.Interval = Consts.TimerDeviceEventInterval;
                        }
                        continue;
                    }
                    if (en.LEDTextHeight == 0 || en.LEDTextWidth == 0)
                        continue;
                    var lbl = new Label();
                    lbl.Tag = en.LEDType;
                    switch ((Enums.Enum_LEDType)en.LEDType)
                    {
                        case Enums.Enum_LEDType.CommanyName:
                            lbl.Text = Session._CommanyName;
                            break;
                        case Enums.Enum_LEDType.SystemName:
                            lbl.Text = Consts.System_Name;
                            break;
                        case Enums.Enum_LEDType.CustomText:
                            lbl.Text = en.LEDText;
                            break;
                        case Enums.Enum_LEDType.CurrentTime:
                            if (_ListCurrtimeLabel == null)
                            {
                                _ListCurrtimeLabel = new List<Label>();
                                this.timerCurrTime.Interval = 1000;
                                this.timerCurrTime.Enabled = true;
                            }
                            _ListCurrtimeLabel.Add(lbl);
                            break;
                        case Enums.Enum_LEDType.EventPhoto:
                            break;
                        //case Enums.Enum_LEDType.ConstructionName:
                        //    return "刷卡员工工地名称";
                        //case Enums.Enum_LEDType.SubContractorName:
                        //    return "刷卡员工分包商";
                        //case Enums.Enum_LEDType.EmployeeName:
                        //    return "刷卡员工姓名";
                        //case Enums.Enum_LEDType.EmployeeNo:
                        //    return "刷卡员工工号";
                        //case Enums.Enum_LEDType.EventTime:
                        //    return "刷卡时间" + DateTime.Now.ToString(); 
                        default:
                            if (_ListEventLabel == null)
                            {
                                _ListEventLabel = new List<Label>();
                                timerEvent.Enabled = true;
                                timerEvent.Interval = Consts.TimerDeviceEventInterval;
                            }
                            _ListEventLabel.Add(lbl);
                            break;
                    }
                    lbl.Font = new Font(DevExpress.Utils.AppearanceObject.DefaultFont.FontFamily, en.FontSize);
                    lbl.ForeColor = Color.FromName(en.ColorName);
                    lbl.Location = new Point(en.LocationX * 2, en.LocationY * 2);
                    lbl.Width = en.LEDTextWidth * 2;
                    lbl.Height = en.LEDTextHeight;
                    lbl.BackColor = System.Drawing.Color.Transparent;
                    this.plContent.Controls.Add(lbl);
                }
                this.BringToFront();
            }
            catch (Exception ex)
            {
                new Loger().LogException(ex);
            }
            finally
            {
                //if (_CallBack != null)
                //    _CallBack();
            }
        }
        private void timerEvent_Tick(object sender, EventArgs e)
        {
            try
            {
                if (_PicEventPhoto != null)
                    _PicEventPhoto.Image = Session.EventImage;
                if (_ListEventLabel == null || _ListEventLabel.Count < 1 || Session.ListADSEventDataToDisplay.Count < 1)
                    return;
                var enDisplay = Session.ListADSEventDataToDisplay[Session.ListADSEventDataToDisplay.Count - 1];
                if (enDisplay == null)
                    return;
                foreach (var lbl in _ListEventLabel)
                {
                    switch ((Enums.Enum_LEDType)lbl.Tag)
                    {
                        case Enums.Enum_LEDType.ConstructionName:
                            lbl.Text = enDisplay.SiteName;
                            break;
                        case Enums.Enum_LEDType.SubContractorName:
                            lbl.Text = enDisplay.Dept_Name;
                            break;
                        case Enums.Enum_LEDType.EmployeeName:
                            lbl.Text = enDisplay.EmplyeeName;
                            break;
                        case Enums.Enum_LEDType.EmployeeNo:
                            lbl.Text = enDisplay.Emp_NO;
                            break;
                        case Enums.Enum_LEDType.EventTime:
                            lbl.Text = enDisplay.Event_Time.ToString(Consts.StrDateTimeFormat);
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                new Loger().LogException(ex);
            }
        }

        private void timerCurrTime_Tick(object sender, EventArgs e)
        {
            if (_ListCurrtimeLabel == null)
                return;
            _ListCurrtimeLabel.ForEach(lbl =>
            {
                lbl.Text = DateTime.Now.ToString(Consts.StrDateTimeFormat);
            });
        }
    }
}
